【问题标题】:Android Mediaplayer play Local Files with VariableAndroid Mediaplayer 播放带变量的本地文件
【发布时间】:2012-05-03 20:12:33
【问题描述】:

想知道是否有人可以帮助我弄清楚如何在 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();
        }
    };
}

【问题讨论】:

    标签: android json media-player local


    【解决方案1】:

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

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

    import android.content.res.AssetFileDescriptor;

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

     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 会告诉您出了什么问题。如果没有,那么我们将需要专业知识。但是你不能责怪一个菜鸟女孩的尝试,是吗?

    【讨论】:

    • 嗨,是的,肯定需要帮助来实现......会发生什么是一个在线控制面板构建 Json 告诉应用程序显示什么......所以如果我有这个变量用于本地文件和本地文件在资产文件夹中,如何将其连接在一起?非常新,感谢您的帮助!
    • Crud ..你在大声笑之前没有提到 Json ..我不知道你好世界是什么,以前从未使用过它。在我为你的“特殊”问题寻找解决方案的时候坚持住 :)
    • 谢谢!我真的很感谢你的帮助……也许我对这一切太陌生了……待命:)
    • 我同意哈哈。你应该让它变得简单。当您可以拥有内置的音频/电影播放器​​时,为什么要使用 Json 和变量?我对 Json 一无所知。如果您可以让该代码在您的代码中工作,我为您鼓掌。我所知道的是“那个”是你播放本地文件的方式。大声笑。
    • 我会提前告诉你 - 你的问题需要由高级开发人员回答。高级,我的意思不是像我们这样的菜鸟 :p 至少没有人可以指责我,因为我是女孩,我们会犯错误:)
    猜你喜欢
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多