web-dev-qa-db-ja.com

Android Media Stream Error? Java.io.FileNotFoundException:コンテンツプロバイダーなし:http://

this に従ってAndroidでストリームラジオを再生しました

ここでは正常に動作しますが、クリック後にプレーヤーの読み込みビットが遅くなります30秒以上待つ必要があります

しかし、コンソールでこのエラーが発生しています

MediaPlayer: setDataSource IOException happend : 
Java.io.FileNotFoundException: No content provider: http://www.example.com:8000/live.ogg
at Android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.Java:1074)
at Android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.Java:927)
at Android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.Java:854)
at Android.media.MediaPlayer.setDataSource(MediaPlayer.Java:1087)
at Android.media.MediaPlayer.setDataSource(MediaPlayer.Java:1061)
at org.oucho.radio.Player.playLaunch(Player.Java:237)
at org.oucho.radio.Playlist.onPostExecute(Playlist.Java:98)
at org.oucho.radio.Playlist.onPostExecute(Playlist.Java:35)
at Android.os.AsyncTask.finish(AsyncTask.Java:632)
at Android.os.AsyncTask.access$600(AsyncTask.Java:177)
at Android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.Java:645)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:145)
at Android.app.ActivityThread.main(ActivityThread.Java:5951)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1400)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1195)

リンクでは player などのようなすべてのファイルを見ることができます

このエラーのため、私のストリームは遅いです。このタイプで誰か助けてください

ここで、このエラーは.oggファイル.mp3およびJust /live

http://www.example.com:8000/beet.ogg
http://www.example.com:8000/mouthorgan.mp3
http://www.example.com:8000/live

オーディオは再生されていますが、このエラーの後、約30秒かかります時々時間がかかりすぎます....再生すると、このエラーが表示され、サーバーに接続して再生されます

これを修正するのを手伝ってください

25
Whats Going On

Java.io.FileNotFoundException:コンテンツプロバイダーなし: http://www.example.com:8000/live.ogg

コンテキストに基づいてデバイスコンテンツプロバイダーからファイルとしてロードしようとしているため、URLとして渡しています。

データソースをmediaplayerに設定することに問題があるようです。 setDataSource コンテキストなしのメソッドに必要なURLまたはストリーミングを再生しようとしているため。

ライブストリーミングの再生中に同じ問題に直面していました。以下のコードで問題が解決しました。

PlayerScreen

        public class PlayerScreen extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.player);
            startService(new Intent(this, MediaPlayerService.class));
            findViewById(R.id.btnChangeTrack).setOnClickListener(clickListener);
            findViewById(R.id.btnStartMediaPlayer).setOnClickListener(clickListener);
            findViewById(R.id.btnStopMediaPlayer).setOnClickListener(clickListener);
            ToggleButton toggleButton = (ToggleButton) findViewById(R.id.togglePauseResume);
            toggleButton.setOnCheckedChangeListener(checkedChangeListener);
            /*
            * To get url which is passing from the previous activity listitem click.
            * If url which is pass from listitem click is not empty it will start player
            * */
            String url = getIntent().getStringExtra("url");
            if (!TextUtils.isEmpty(url))
                startMediaPlayer(url);

        }

        private ToggleButton.OnCheckedChangeListener checkedChangeListener = new ToggleButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    Intent intent = new Intent();
                    intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                    intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PAUSE_MEDIA_PLAYER);
                    sendBroadcast(intent);
                } else {
                    Intent intent = new Intent();
                    intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                    intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.RESUME_MEDIA_PLAYER);
                    sendBroadcast(intent);
                }
            }
        };
        private View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                switch (v.getId()) {
                    case R.id.btnChangeTrack:
                        intent = new Intent();
                        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.CHANGE_PLAYER_TRACK);
                        intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, "http://www.example.com:8000/live");
                        sendBroadcast(intent);
                        break;
                    case R.id.btnStartMediaPlayer:
                        startMediaPlayer("http://www.example.com:8000/beet.ogg");
//startMediaPlayer("http://108.163.197.114:8071/listen.pls");
                        break;
                    case R.id.btnStopMediaPlayer:
                        intent = new Intent();
                        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.STOP_MEDIA_PLAYER);
                        sendBroadcast(intent);
                        break;

                }
            }
        };

        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(receiverFromservice, new IntentFilter(MediaPlayerService.SERVICE_TO_ACTIVITY));
        }

        private String currentPlayerStatus = "N/A";
        private BroadcastReceiver receiverFromservice = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (MediaPlayerService.SERVICE_TO_ACTIVITY.equalsIgnoreCase(action)) {
                    /*
                    * To get current status of player
                    * */
                    currentPlayerStatus = intent.getStringExtra(MediaPlayerService.PLAYER_STATUS_KEY);
                    Log.e("Player Mode", "" + currentPlayerStatus);
                }
            }
        };

        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(receiverFromservice);
        }

        /**
         * TO start media player.It will send broadcast to Service & from service player will start
         *
         * @param url
         */
        public void startMediaPlayer(String url) {
            Intent intent = new Intent();
            intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
            intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PLAY_MEDIA_PLAYER);
            intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, url);
            sendBroadcast(intent);
        }
    }

MediaPlayerService

public class MediaPlayerService extends Service {
    public static final String BROADCAST_TO_SERVICE = "com.mediaplayer.playerfunction";
    public static final String SERVICE_TO_ACTIVITY = "com.mediaplayer.currentPlayerStatus";
    public static final String PLAYER_FUNCTION_TYPE = "playerfunction";
    public static final String PLAYER_TRACK_URL = "trackURL";
    public static final int PLAY_MEDIA_PLAYER = 1;
    public static final int PAUSE_MEDIA_PLAYER = 2;
    public static final int RESUME_MEDIA_PLAYER = 3;
    public static final int STOP_MEDIA_PLAYER = 4;
    public static final int CHANGE_PLAYER_TRACK = 5;
    public static final String PLAYER_STATUS_KEY = "PlayerCurrentStatus";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        IntentFilter intentFilter = new IntentFilter(BROADCAST_TO_SERVICE);
        registerReceiver(playerReceiver, intentFilter);
        if (mPlayer != null && mPlayer.isPlaying()) {
            sendPlayerStatus("playing");
        }
        return START_STICKY;
    }

    private BroadcastReceiver playerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BROADCAST_TO_SERVICE.equalsIgnoreCase(action)) {
                String trackURL = intent.hasExtra(PLAYER_TRACK_URL) ? intent.getStringExtra(PLAYER_TRACK_URL) : "";
                int function = intent.getIntExtra(PLAYER_FUNCTION_TYPE, 0);
                switch (function) {
                    case CHANGE_PLAYER_TRACK:
                        changeTrack(trackURL);
                        break;
                    case STOP_MEDIA_PLAYER:
                        stopPlayer();
                        break;
                    case PLAY_MEDIA_PLAYER:
                        startMediaPlayer(trackURL);
                        break;
                    case PAUSE_MEDIA_PLAYER:
                        pausePlayer();
                        break;
                    case RESUME_MEDIA_PLAYER:
                        resumePlayer();
                        break;
                }

            }
        }
    };
    private MediaPlayer mPlayer;

    private void pausePlayer() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.pause();
            sendPlayerStatus("pause");
        }
    }

    private void resumePlayer() {
        if (mPlayer != null && !mPlayer.isPlaying()) {
            mPlayer.start();
            sendPlayerStatus("playing");
        }
    }

    private void changeTrack(String url) {
        stopPlayer();
        startMediaPlayer(url);

    }

    private void stopPlayer() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
            sendPlayerStatus("stopped");

        }
    }

    public void startMediaPlayer(String url) {
        if (TextUtils.isEmpty(url))
            return;
        if (mPlayer == null)
            mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(url);
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (extra == MediaPlayer.MEDIA_ERROR_SERVER_DIED
                            || extra == MediaPlayer.MEDIA_ERROR_MALFORMED) {
                        sendPlayerStatus("erroronplaying");
                    } else if (extra == MediaPlayer.MEDIA_ERROR_IO) {
                        sendPlayerStatus("erroronplaying");
                        return false;
                    }
                    return false;
                }
            });
            mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Log.e("onBufferingUpdate", "" + percent);

                }
            });
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                public void onPrepared(MediaPlayer mp) {
                    mPlayer.start();
                    sendPlayerStatus("playing");
                }
            });
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.e("onCompletion", "Yes");
                    sendPlayerStatus("completed");
                }
            });
            mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    return false;
                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendPlayerStatus(String status) {
        Intent intent = new Intent();
        intent.setAction(SERVICE_TO_ACTIVITY);
        intent.putExtra(PLAYER_STATUS_KEY, status);
        sendBroadcast(intent);
    }
}

player.xml

<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <TextView
        Android:id="@+id/section_label"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_below="@+id/ab_tool"
        Android:text="Home" />

    <Button
        Android:id="@+id/btnStartMediaPlayer"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_below="@+id/section_label"
        Android:text="Start Player" />


    <ToggleButton
        Android:id="@+id/togglePauseResume"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_below="@+id/btnStartMediaPlayer"
        Android:checked="true"
        Android:textOff="Resume"
        Android:textOn="Pause" />

    <Button
        Android:id="@+id/btnChangeTrack"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_below="@+id/togglePauseResume"
        Android:text="Chanage Track" />

    <Button
        Android:id="@+id/btnStopMediaPlayer"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_below="@+id/btnChangeTrack"
        Android:text="STOP" />
</RelativeLayout>

マニフェスト

<activity Android:name=".PlayerScreen">

    <intent-filter>
        <action Android:name="Android.intent.action.MAIN" />


        <category Android:name="Android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<service Android:name=".MediaPlayerService"></service>

ストリーミングurlヘッダーデータからデータを取得するには、[この回答を確認] [2]

ここでテストするために、2つのURLを使用しました

UPDATEPlayerActivity

public class PlayerScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        startService(new Intent(this, MediaPlayerService.class));
        /*
        * To get url which is passing from the previous activity listitem click.
        * If url which is pass from listitem click is not empty it will start player
        * */
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url))
            startMediaPlayer(url);

    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiverFromservice, new IntentFilter(MediaPlayerService.SERVICE_TO_ACTIVITY));
    }

    private String currentPlayerStatus = "N/A";
    private BroadcastReceiver receiverFromservice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MediaPlayerService.SERVICE_TO_ACTIVITY.equalsIgnoreCase(action)) {
                /*
                * To get current status of player
                * */
                currentPlayerStatus = intent.getStringExtra(MediaPlayerService.PLAYER_STATUS_KEY);
                Log.e("Player Mode", "" + currentPlayerStatus);
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiverFromservice);
    }

    /**
     * TO start media player.It will send broadcast to Service & from service player will start
     *
     * @param url
     */
    public void startMediaPlayer(String url) {
        Intent intent = new Intent();
        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PLAY_MEDIA_PLAYER);
        intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, url);
        sendBroadcast(intent);
    }
}

何かあれば教えてください。

15
user1140237

このファイルに移動します https://github.com/Old-Geek/Radio/blob/master/app/src/main/Java/org/oucho/radio/Player.Java#L234 と変更

player.setDataSource(context, Uri.parse(url));

player.setDataSource(url)

問題は、void setDataSource (String path)が使用するデータソース(ファイルパスまたはhttp/rtsp URL)を設定することです。

path文字列:ファイルのパス、または再生するストリームのhttp/rtsp URL

githubコードはvoid setDataSource (Context context, Uri uri)を使用します。これはuriが何らかの形式のContentProviderであると想定しています

context Context:Uriを解決するときに使用するコンテキスト
uri Uri:再生するデータのコンテンツURI

29
Anurag Singh

Android 9+では、クリアな純HTTPトラフィックがこの問題を引き起こす可能性があります。これを確認してください: Android 8:Cleartext HTTP traffic not allowed

6
Yazon2006

このエラーは、このインターネット許可がManifest.xmlにない場合にも発生します

<uses-permission Android:name="Android.permission.INTERNET" />
1
s-hunter