0

想知道是否有人可以帮助我弄清楚如何在 Android Mediaplayer 中播放本地音频/视频文件...我可以使用保存 URL 地址的变量从 URL 流式传输音频/视频,但是如何处理本地文件?如何使用localFile变量调用本地Files进行播放?感谢大家!

package com.ryanleckey2;






import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;


public class Audio_Video_Stream extends  BT_activity_base{


    private String localFile = "";
    private String localFileName = "";
    private String dataURL = "";
    private String currentURL = "";
    public String thisActivityName = "Screen_Video";

    //movie controller
    String videoUrl;
    VideoView videoView;
    MediaController mediaController;
    public int isLoading;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_video);



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

        localFileName = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "localFileName", "");


                //setup the saveAsFileName
                if(localFileName.length() > 1){

                    //use the file name in the JSON data...
                localFile = localFileName;  





        //Log.i("ZZ", thisActivityName + ":videoUrl : " + videoUrl);   

        videoView = (VideoView) findViewById(R.id.videoView);
        mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        isLoading = 0;
        }

    }   










    ///////////////////////////////////////////////////
    //activity life-cycle overrides

    //onStart
    @Override 
    protected void onStart(){
        super.onStart();
        //Log.i("ZZ", thisActivityName + ":onStart");
    }

    //onResume
    @Override
    public void onResume() {
        super.onResume();

        if(isLoading == 1){
            if(videoView != null){
                videoView = null;
            }
        }
        startMovie();
        //Log.i("ZZ", thisActivityName + ":onResume");
    }

    //onPause
    @Override
    public void onPause() {
        //Log.i("ZZ", thisActivityName + ":onPause");
        super.onPause();

        if(videoView != null){
            videoView = null;
        }
    }

    //onStop
    @Override 
    protected void onStop(){
        super.onStop();

        //Log.i("ZZ", thisActivityName + ":onStop");
    }   

    //onDestroy
    @Override
    public void onDestroy() {
        //Log.i("ZZ", thisActivityName + ":onDestroy");
        super.onDestroy();
    }

    //activity life-cycle overrides
    ///////////////////////////////////////////////////


    //starts movie
    public void startMovie(){

        //show progress for a few seconds..
        showProgress("Starting media stream...", "Please be patient. If you're on 3G this could take a bit.\nWi-Fi is much better for streaming.");
        isLoading = 1;

        try{
            //set audio/video link (mp4,mp3 format )
            mediaController = new MediaController(this);
            Uri uri = Uri.parse(currentURL);
            //Uri       uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.);  
            videoView.setMediaController(mediaController);
            videoView.setVideoURI(uri);        
            videoView.setOnPreparedListener(mOnPreparedListener);

            videoView.start();

        }catch(Exception je){
        hideProgress();
            showAlert("Invalid URL","The URL to the media could not be determined. This screen will close.");
            finish();
        }

    }

    //onPrepared...
    private MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener(){
        public void onPrepared(MediaPlayer mp){
            //Log.i("ZZ", thisActivityName + ":OnPreparedListener");
            isLoading = 0;
        //  mp.setLooping(true);
        hideProgress();
        }
    };
}
4

1 回答 1

1

我使用媒体播放器播放位于我的资产文件夹中的本地音频文件。这是我的代码片段。如果您需要帮助实施,请告诉我。这些代码行位于 try 方法中。

更新好的,我在这里在黑暗中拍摄,但让我们看看你是否可以用我的方法切换你的尝试方法。首先,使用这个导入:

import android.content.res.AssetFileDescriptor;

现在,在你的“尝试”括号内,试试这个:

 AssetFileDescriptor afd = getAssets().openFd("YOURSOUNDORVIDEO.mp3/mpg");  
 mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
 mp.prepare();
 mp.start(); //play sound or video

在我的代码中,mp 代表媒体播放器。将其更改为您用来描述本地文件或播放器的任何内容,看看它是否有效。如果出现问题,Android 会告诉您出了什么问题。如果没有,那么我们将需要专业知识。但是你不能责怪一个菜鸟女孩的尝试,是吗?

于 2012-05-03T21:11:41.067 回答