我想通过firebase和mediaplayer流式传输mp3。我在firebase存储中保存我的音乐,并将他们的downloadUrl传递给我的媒体播放器实例。我正在列出所有歌曲并选择其中一首并开始播放,但是在选定的歌曲完成后,它会出现错误
这是我的代码
/**
* Called when the service is being created.
*/
@Override
public void onCreate() {
super.onCreate();
Log.i("Service Demo", "Service Created");
player = new MediaPlayer();
initMusicPlayer();
}
// to initialize the media class
public void initMusicPlayer() {
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
}
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
mp.start();
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.i("TEST", "Service, song end");
playNext();
}
public void playSong() {
//play a song
player.reset();
String songUrl = mCurrentPlayList.get(mCurrentSongPos).getSongUrl();
try {
player.setDataSource(getApplicationContext(), Uri.parse(songUrl));
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
player.prepareAsync();
}
// Skip to next song
public void playNext(){
if(checkConnection()) {
mCurrentSongPos++;
if (mCurrentSongPos >= mCurrentPlayList.size()) {
mCurrentSongPos = 0;
}
playSong();
Log.i("TEST", "Next Song is playing");
}
}
// Back to previsous song
public void playPrev(){
if(checkConnection()) {
mCurrentSongPos--;
if (mCurrentSongPos < 0) {
mCurrentSongPos = mCurrentPlayList.size() - 1;
}
playSong();
Log.i("TEST", "Previous Song is playing");
}
}
这是我的logcat
D/MediaPlayer: setDataSource IOException happend :
java.io.FileNotFoundException: No content provider: https://firebasestorage.googleapis.com/v0/b/masalzamani-60c94.appspot.com/o/Songs%2FEzop%20Masallar%C4%B1%2FEzop%20Masallar%C4%B1%203%20-%20Aslan%20Postu%20Giyen%20E%C5%9Fek.mp3?alt=media&token=4c298a8f-7bb0-4cbc-8c82-82ccc51c24a7
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:1085)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
at ysfcyln.masalzamani.LocalService.playSong(LocalService.java:303)
at ysfcyln.masalzamani.LocalService.playNext(LocalService.java:477)
at ysfcyln.masalzamani.LocalService.onCompletion(LocalService.java:232)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3346)
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)
06-17 18:27:06.841 29201-29201/ysfcyln.masalzamani D/MediaPlayer: Couldn't open file on client side, trying server side
06-17 18:27:06.861 29201-29201/ysfcyln.masalzamani V/MediaPlayer: setVideoSurfaceTexture
06-17 18:27:06.861 29201-29201/ysfcyln.masalzamani V/MediaPlayer: prepareAsync
06-17 18:27:06.866 29201-29201/ysfcyln.masalzamani V/BitmapFactory: DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/row_music_icon.png
06-17 18:27:06.881 29201-29214/ysfcyln.masalzamani I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-17 18:27:06.921 29201-29201/ysfcyln.masalzamani I/TEST: Next Song is playing
06-17 18:27:07.451 29201-29201/ysfcyln.masalzamani V/MediaPlayer-JNI: isPlaying: 0
06-17 18:27:07.776 29201-29214/ysfcyln.masalzamani V/MediaHTTPConnection: mTotalSize is 5179086
06-17 18:27:07.786 29201-29217/ysfcyln.masalzamani D/MediaHTTPConnection: setReadTimeOut = 30000ms
06-17 18:27:08.451 29201-29201/ysfcyln.masalzamani V/MediaPlayer-JNI: isPlaying: 0
06-17 18:27:08.591 29201-29218/ysfcyln.masalzamani V/MediaPlayer: message received msg=3, ext1=15, ext2=0
06-17 18:27:08.591 29201-29218/ysfcyln.masalzamani V/MediaPlayer: buffering 15
06-17 18:27:08.591 29201-29218/ysfcyln.masalzamani V/MediaPlayer: callback application
06-17 18:27:08.591 29201-29218/ysfcyln.masalzamani V/MediaPlayer: back from callback
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani V/MediaPlayer: getDuration_l
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani V/MediaPlayer: message received msg=100, ext1=-38, ext2=0
06-17 18:27:08.596 29201-29218/ysfcyln.masalzamani V/MediaPlayer: message received msg=200, ext1=973, ext2=0
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani E/MediaPlayer: error (-38, 0)
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani V/MediaPlayer: callback application
06-17 18:27:08.596 29201-29218/ysfcyln.masalzamani W/MediaPlayer: info/warning (973, 0)
06-17 18:27:08.596 29201-29201/ysfcyln.masalzamani V/MediaPlayer: back from callback
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: callback application
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: back from callback
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: message received msg=1, ext1=0, ext2=0
06-17 18:27:08.601 29201-29201/ysfcyln.masalzamani V/MediaPlayer-JNI: getDuration: 0 (msec)
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: prepared
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: callback application
06-17 18:27:08.601 29201-29218/ysfcyln.masalzamani V/MediaPlayer: back from callback
06-17 18:27:08.611 29201-29201/ysfcyln.masalzamani E/MediaPlayer: Error (-38,0)
06-17 18:27:08.611 29201-29201/ysfcyln.masalzamani I/TEST: Service, song end
06-17 18:27:08.646 29201-29201/ysfcyln.masalzamani V/MediaPlayer-JNI: reset
06-17 18:27:08.646 29201-29201/ysfcyln.masalzamani V/MediaPlayer: reset
06-17 18:27:08.646 29201-30675/ysfcyln.masalzamani V/MediaPlayer: message received msg=8, ext1=0, ext2=0
06-17 18:27:08.666 29201-29201/ysfcyln.masalzamani D/MediaPlayer: setDataSource IOException happend :
java.io.FileNotFoundException: No content provider: https://firebasestorage.googleapis.com/v0/b/masalzamani-60c94.appspot.com/o/Songs%2FEzop%20Masallar%C4%B1%2FEzop%20Masallar%C4%B1%204%20-%20Aslan%2C%20Kurt%20ve%20Tilki.mp3?alt=media&token=8da077ee-bad5-4688-9833-caf8e15daf05
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:1085)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
at ysfcyln.masalzamani.LocalService.playSong(LocalService.java:303)
at ysfcyln.masalzamani.LocalService.playNext(LocalService.java:477)
at ysfcyln.masalzamani.LocalService.onCompletion(LocalService.java:232)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3478)
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)
06-17 18:27:08.666 29201-29201/ysfcyln.masalzamani D/MediaPlayer: Couldn't open file on client side, trying server side
06-17 18:27:08.666 29201-30675/ysfcyln.masalzamani V/MediaPlayer: notify(8, 0, 0) callback on disconnected mediaplayer
06-17 18:27:08.671 29201-29201/ysfcyln.masalzamani V/MediaPlayer: setVideoSurfaceTexture
06-17 18:27:08.671 29201-29201/ysfcyln.masalzamani V/MediaPlayer: prepareAsync
06-17 18:27:08.676 29201-29214/ysfcyln.masalzamani I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-17 18:27:08.681 29201-29214/ysfcyln.masalzamani I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
06-17 18:27:08.686 29201-29201/ysfcyln.masalzamani I/TEST: Next Song is playing
当第一首歌被选中但它成功播放时会出现同样的错误,我不知道为什么其他歌曲没有播放,有人可以帮忙吗?
答案 0 :(得分:0)
我认为它是媒体播放器的常见错误。当我用ExoPlayer切换媒体播放器时,一切正常
对于任何人都有相同的错误,我正在分享ExoPlayer的基本用法
添加ExoPlayer
compile 'com.google.android.exoplayer:exoplayer:r2.4.2'
然后
public class MainActivity extends AppCompatActivity implements ExoPlayer.EventListener{
private BandwidthMeter bandwidthMeter;
private TrackSelector trackSelector;
private TrackSelection.Factory trackSelectionFactory;
private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private DefaultBandwidthMeter defaultBandwidthMeter;
private MediaSource mediaSource;
private String songUrl = "http://www.mfiles.co.uk/mp3-downloads/edvard-grieg-peer-gynt1-morning-mood-piano.mp3";
private Button stopButton;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.btnStart);
stopButton = (Button) findViewById(R.id.btnStop);
bandwidthMeter = new DefaultBandwidthMeter();
extractorsFactory = new DefaultExtractorsFactory();
trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
defaultBandwidthMeter = new DefaultBandwidthMeter();
dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "mediaPlayerSample"),defaultBandwidthMeter);
mediaSource = new ExtractorMediaSource(Uri.parse(songUrl),
dataSourceFactory,
extractorsFactory,
null,
null);
player = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
player.addListener(this);
player.prepare(mediaSource);
Log.v("TEST","playing state : " + player.getPlaybackState());
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setPlayWhenReady(true);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setPlayWhenReady(false);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
player.setPlayWhenReady(false);
}
@Override
public void onLoadingChanged(boolean isLoading) {
Log.i("TEST", "onLoadingChanged: " + isLoading + "");
Log.i("TEST", "Buffered Position: " + player.getBufferedPosition() + "");
Log.i("TEST", "Buffered Percentage: " + player.getBufferedPercentage() + "");
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if(playbackState == ExoPlayer.STATE_READY){
Log.i("TEST", "ExoPlayer State is: READY");
} else if (playbackState == ExoPlayer.STATE_BUFFERING){
Log.i("TEST", "ExoPlayer State is: BUFFERING");
} else if (playbackState == ExoPlayer.STATE_ENDED){
Log.i("TEST", "ExoPlayer State is: ENDED");
} else if (playbackState == ExoPlayer.STATE_IDLE){
Log.i("TEST", "ExoPlayer State is: IDLE");
}
}
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
@Override
public void onPositionDiscontinuity() {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
}
答案 1 :(得分:0)
在getApplicationContext()
调用中使用player.setDataSource(getApplicationContext(), Uri.parse(songUrl));
使MediaPlayer
首先在本地(emem)(由于指定的应用程序上下文),然后在出现以下情况时尝试网络它找不到它。
以下过载似乎可以避免这种情况:
player.setDataSource(songUrl);