强制关闭设备Android Mediaplayer

时间:2012-05-26 23:08:05

标签: android media-player

我正在处理下面链接中的代码...它可以在模拟器上运行但是在设备上强制关闭...我相信它会超时导致强制关闭...不确定在何处/如何实现进度指示器如果那是什么需要的?对此非常陌生......谢谢!

我将代码行替换为来自网址的流...所以我确信问题来自哪里

mediaPlayer.setDataSource(audioFile);

更改为

mediaPlayer.setDataSource(dataURL);

How can I show a MediaController while playing audio in Android?

以下是完整代码:

package com.windmillagency;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import java.io.IOException;

public class Audio_Image extends BT_activity_base implements OnPreparedListener, MediaController.MediaPlayerControl{
      private String dataURL = "";

        public String thisActivityName = "Audio_Image";


    private static final String TAG = "AudioPlayer";

  public static final String AUDIO_FILE_NAME = "audioFileName";

  private MediaPlayer mediaPlayer;
  private MediaController mediaController;
  private String audioFile;

  private Handler handler = new Handler();

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.activityName = "Audio_Image";
    BT_debugger.showIt(activityName + ":onCreate");

  //reference to base layout..
    LinearLayout baseView = (LinearLayout)findViewById(R.id.baseView);

    //setup background colors...
    BT_viewUtilities.updateBackgroundColorsForScreen(this, this.screenData);

    //setup background images..
    if(backgroundImageWorkerThread == null){
        backgroundImageWorkerThread = new BackgroundImageWorkerThread();
        backgroundImageWorkerThread.start();
    }           

    //setup navigation bar...
    LinearLayout navBar = BT_viewUtilities.getNavBarForScreen(this, this.screenData);
    if(navBar != null){
        baseView.addView(navBar);
    }


    //inflate this screens layout file...
    LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View thisScreensView = vi.inflate(R.layout.audio_image, null);

    //add the view to the base view...
    baseView.addView(thisScreensView);


  //fill JSON properties...
        dataURL = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "dataURL", "");

        //figure out what to load...

    audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);


    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);

    mediaController = new MediaController(this);





    try {
      mediaPlayer.setDataSource(dataURL);
      mediaPlayer.prepare();
      mediaPlayer.start();
    } catch (IOException e) {

      Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
    }




  }

  @Override
  protected void onStop() {
    super.onStop();
    mediaPlayer.stop();
    mediaPlayer.release();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    mediaController.show();
    return false;
  }

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() { 
    mediaPlayer.start();
  }

  public void pause() {
    mediaPlayer.pause();
  }

  public int getDuration() {
    return mediaPlayer.getDuration();
  }

  public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
  }

  public void seekTo(int i) {
    mediaPlayer.seekTo(i);
  }

  public boolean isPlaying() {
    return mediaPlayer.isPlaying();
  }

  public int getBufferPercentage() {
    return 0;
  }

  public boolean canPause() {
    return true;
  }

  public boolean canSeekBackward() {
    return true;
  }

  public boolean canSeekForward() {
    return true;
  }
  //--------------------------------------------------------------------------------

  public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);


    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

    handler.post(new Runnable() {
      public void run() {
        mediaController.setEnabled(true);
        mediaController.show();
      }
    });
  }
}




<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/main_audio_view"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:orientation="vertical"
              android:padding="40dip" >

</LinearLayout>

0 个答案:

没有答案