Android的使用进度为音频 [英] Android use progressBar for the Audio

查看:329
本文介绍了Android的使用进度为音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序许多声音。它的互联网网址所运行>>

我想设置进度条在我的应用程序?

我怎么办呢?

这是合理的活动:

 <?XML版本=1.0编码=UTF-8&GT?; < RelativeLayout的
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     的xmlns:工具=htt​​p://schemas.android.com/tool​​s
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     机器人:背景=@绘制/背景
     机器人:方向=垂直
     工具:忽略=ScrollViewSize>     <的MediaController
         机器人:ID =@ + ID / mediaController1
         机器人:layout_width =WRAP_CONTENT
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_centerHorizo​​ntal =真
         机器人:layout_marginBottom =66dp
         机器人:layout_marginRight =14dp>
     < /&的MediaController GT;     <滚动型
         机器人:ID =@ + ID / scrollView1
         机器人:layout_width =match_parent
         机器人:layout_height =match_parent
         机器人:layout_alignTop =@ + ID /播放
         机器人:layout_centerHorizo​​ntal =真
         工具:忽略=NotSibling>         < RelativeLayout的
             机器人:layout_width =match_parent
             机器人:layout_height =316dp
             机器人:方向=垂直>             < ImageView的
                 机器人:ID =@ + ID / imageView1
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentRight =真
                 机器人:layout_alignParentTop =真
                 机器人:paddingBottom会=90dp
                 机器人:paddingTop =50dp
                 机器人:SRC =@绘制/ logo5
                 工具:忽略=ContentDescription/>             <的ImageButton
                 机器人:ID =@ + ID /播放
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentBottom =真
                 机器人:layout_centerHorizo​​ntal =真
                 机器人:SRC =@绘制/ ic_action_play
                 工具:忽略=ContentDescription,ObsoleteLayoutParam/>             <的TextView
                 机器人:ID =@ + ID / textView1
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentTop =真
                 机器人:layout_centerHorizo​​ntal =真
                 机器人:layout_marginTop =25dp
                 机器人:文字=@字符串/松1
                 机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?             <的ImageButton
                 机器人:ID =@ + ID / dawnload
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentBottom =真
                 机器人:layout_toRightOf =@ + ID /播放
                 机器人:SRC =@绘制/ ic_action_download
                 工具:忽略=ContentDescription,ObsoleteLayoutParam/>             <的ImageButton
                 机器人:ID =@ + ID / sahere
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentBottom =真
                 机器人:layout_toLeftOf =@ + ID /播放
                 机器人:SRC =@绘制/ ic_action_share
                 工具:忽略=ContentDescription,ObsoleteLayoutParam/>
        < / RelativeLayout的>
    < /滚动型>
< / RelativeLayout的>

也这是声音类

 包com.Helmi_wa_ana;进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口android.app.Activity;
进口android.content.Intent;
进口android.media.MediaPlayer;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;公共类Sound1例子延伸活动{    @覆盖保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.sound1);
        最终的MediaPlayer mediaController1 =新的MediaPlayer();
        尝试{
            mediaController1.setDataSource(http://alshbab90.softsmedia.com/helmi_1.mp3);
        }赶上(抛出:IllegalArgumentException E1){
            e1.printStackTrace();
        }赶上(E1 IllegalStateException异常){
            e1.printStackTrace();
        }赶上(IOException异常E1){
            e1.printStackTrace();
        }        尝试{
            mediaController1 prepare()。
        }赶上(IllegalStateException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }        玩的ImageButton =(的ImageButton)findViewById(R.id.play);
        play.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                如果(mediaController1.isPlaying()){
                    mediaController1.pause();
                }其他{
                    mediaController1.start();
                }
            }
        });        的ImageButton sahere =(的ImageButton)findViewById(R.id.sahere);
        sahere.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v)
            {
                意图sharingIntent =新意图(Intent.ACTION_SEND);
                sharingIntent.setType(text / plain的);
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,getResources()的getString(R.string.share1));
                startActivity(Intent.createChooser(sharingIntent,?????? ?????? ????????));
            }
        });        的ImageButton dawnolad =(的ImageButton)findViewById(R.id.dawnload);
        dawnolad.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
            }            受保护的InputStream的OpenURL(){
                字符串URL =htt​​p://alshbab90.softsmedia.com/helmi_1.mp3;
                InputStream的流= NULL;
                HttpURLConnection的连接= NULL;
                尝试{
                    连接=(HttpURLConnection类)新的URL(网址).openConnection();
                }赶上(MalformedURLException的E1){
                    e1.printStackTrace();
                }赶上(IOException异常E1){
                    e1.printStackTrace();
                }
                尝试{
                    如果(connection.getResponse code()== HttpURLConnection.HTTP_OK){
                        流= connection.getInputStream();
                    }
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
                返回流;            }
        });
    }
}


解决方案

喜请尝试以下参考链接,它应该对你有所帮助进度tutorials.Hope。您可以下载示例源代码code和尝试在你的应用程序。

进度条

<一个href=\"http://examples.java$c$cgeeks.com/android/core/ui/progressdialog/android-progressdialog-example/\"相对=nofollow>进度条用于下载文件

的Andr​​oid进度条示例

喜在这里,我添加了从服务器code示例下载文件。请让我知道。

 进口的java.io.InputStream;
    进口的java.net.URL;
    进口android.app.Activity;
    进口android.app.ProgressDialog;
    进口android.os.Bundle;
    进口android.util.Log;
    进口android.view.View;
    进口android.widget.Button;
    进口java.io.BufferedInputStream中;
    进口java.io.FileOutputStream中;
    进口java.io.OutputStream中;
    进口java.net.URLConnection中;
    进口android.app.Dialog;
    进口android.graphics.drawable.Drawable;
    进口android.os.AsyncTask;
    进口android.os.Environment;
    进口android.widget.ImageView;    公共类下载扩展活动{        //按钮显示进度对话框
        按钮btnShowProgress;        //进度对话框
        私人ProgressDialog pDialog;
        ImageView的my_image;
        //进度对话框类型(0 - 对于水平进度条)
        公共静态最终诠释progress_bar_type = 0;        //文件的URL下载
        私人静态字符串FILE_URL =htt​​p://alshbab90.softsmedia.com/helmi_1.mp3;        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        //显示进度条按钮
        btnShowProgress =(按钮)findViewById(R.id.btnProgressBar);
        //图像视图下载后显示图片
        my_image =(ImageView的)findViewById(R.id.my_image);
        / **
         *显示进度条点击事件
         * * /
          btnShowProgress.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
            //开始新的异步任务
            新DownloadFileFromURL()执行(FILE_URL);
            }
        });
        }        / **
         *显示对话框
         * * /
        @覆盖
        保护对话框onCreateDialog(INT ID){
        开关(ID){
        案例progress_bar_type://我们设置它为0
            pDialog =新ProgressDialog(本);
            pDialog.setMessage(下载文件,请稍候...);
            pDialog.setIndeterminate(假);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(真);
            pDialog.show();
            返回pDialog;
        默认:
            返回null;
        }
        }        / **
         *背景异步任务来下载文件
         * * /
        类DownloadFileFromURL扩展的AsyncTask&LT;字符串,字符串,字符串&GT; {        / **
         *启动后台线程之前
         *显示进度条对话框
         * * /
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            的ShowDialog(progress_bar_type);
        }        / **
         在后台线程下载*文件
         * * /
        @覆盖
        保护字符串doInBackground(字符串... f_url){
            诠释计数;
            尝试{
            网址URL =新的URL(f_url [0]);
            URLConnection的连接如= url.openConnection();
            conection.connect();
            //这将是有益的,这样就可以显示tipical 0-100%的进度条
            INT lenghtOfFile = conection.getContentLength();            //下载文件
            InputStream的输入=新的BufferedInputStream(url.openStream(),8192);            //输出流
            OutputStream的输出=新的FileOutputStream(/ SD卡/ helmi_1.mp3);            字节的数据[] =新的字节[1024];            总长= 0;            而((计数= input.read(数据))!= - 1){
                总+ =计数;
                //发布进度....
                //在此之后onProgressUpdate将被称为
                publishProgress(+(int)的((总* 100)/ lenghtOfFile));                //将数据写入到文件
                output.write(数据0,计);
            }            //冲洗输出
            output.flush();            //关闭流
            output.close();
            input.close();            }赶上(例外五){
            Log.e(错误,e.getMessage());
            }            返回null;
        }        / **
         *更新进度条
         * * /
        保护无效onProgressUpdate(字符串...进度){
            //设置进度百分比
            pDialog.setProgress(的Integer.parseInt(进展[0]));
           }        / **
         *在完成后台任务之后
         *辞退进度对话框
         * ** /
        @覆盖
        保护无效onPostExecute(字符串FILE_URL){
            //关闭该对话框的文件下载后
             dismissDialog(progress_bar_type);             //显示下载的图像到图像视图
             //从SD卡读取图像路径
               字符串的ImagePath = Environment.getExternalStorageDirectory()的toString()+/helmi_1.mp3。
             //设置下载到图像视图
            my_image.setImageDrawable(Drawable.createFromPath(的ImagePath));
        }        }
    }

创建 main.xml中:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直&GT;&LT;! - 下载按钮 - &GT;
&LT;按钮机器人:ID =@ + ID / btnProgressBar
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=下载文件与进度条
    机器人:layout_marginTop =50dip/&GT;&LT;! - 图片以下载后显示的图像 - &GT;
&LT; ImageView的机器人:ID =@ + ID / my_image
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/&GT;&LT; / LinearLayout中&GT;

需要补充的Andr​​oidManifest.xml许可为,

 &LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT;
&LT;使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/&GT;

希望它应该对你有帮助。请尝试,让我知道。谢谢

in my app many sounds . its run by Internet by url >>

I want to set progress Bar in my app ?

How I do it ?

this is sound Activity:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@drawable/background"
     android:orientation="vertical"
     tools:ignore="ScrollViewSize" >

     <MediaController
         android:id="@+id/mediaController1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="66dp"
         android:layout_marginRight="14dp" >
     </MediaController>

     <ScrollView
         android:id="@+id/scrollView1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_alignTop="@+id/play"
         android:layout_centerHorizontal="true"
         tools:ignore="NotSibling" >

         <RelativeLayout
             android:layout_width="match_parent"
             android:layout_height="316dp"
             android:orientation="vertical" >

             <ImageView
                 android:id="@+id/imageView1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:layout_alignParentTop="true"
                 android:paddingBottom="90dp"
                 android:paddingTop="50dp"
                 android:src="@drawable/logo5"
                 tools:ignore="ContentDescription" />

             <ImageButton
                 android:id="@+id/play"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_centerHorizontal="true"
                 android:src="@drawable/ic_action_play"
                 tools:ignore="ContentDescription,ObsoleteLayoutParam" />

             <TextView
                 android:id="@+id/textView1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentTop="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginTop="25dp"
                 android:text="@string/song1"
                 android:textAppearance="?android:attr/textAppearanceLarge" />

             <ImageButton
                 android:id="@+id/dawnload"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_toRightOf="@+id/play"
                 android:src="@drawable/ic_action_download"
                 tools:ignore="ContentDescription,ObsoleteLayoutParam" />

             <ImageButton
                 android:id="@+id/sahere"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_toLeftOf="@+id/play"
                 android:src="@drawable/ic_action_share"
                 tools:ignore="ContentDescription,ObsoleteLayoutParam" />
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

also this is sound class

package com.Helmi_wa_ana;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.net.Uri;
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.ImageButton;

public class Sound1 extends Activity {

    @Override protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sound1);
        final MediaPlayer mediaController1 = new MediaPlayer();
        try {
            mediaController1.setDataSource("http://alshbab90.softsmedia.com/helmi_1.mp3");
        } catch (IllegalArgumentException e1) {
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            mediaController1.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ImageButton play = (ImageButton) findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mediaController1.isPlaying()) {
                    mediaController1.pause();
                } else {
                    mediaController1.start();
                }
            }
        });

        ImageButton sahere = (ImageButton) findViewById(R.id.sahere);
        sahere.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share1));
                startActivity(Intent.createChooser(sharingIntent,"?????? ???????? ?????? :"));
            }
        });

        ImageButton dawnolad = (ImageButton) findViewById(R.id.dawnload);
        dawnolad.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            }

            protected InputStream openUrl() {
                String url = "http://alshbab90.softsmedia.com/helmi_1.mp3";
                InputStream stream = null;
                HttpURLConnection connection = null;
                try {
                    connection = (HttpURLConnection)new URL(url).openConnection();
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        stream = connection.getInputStream();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return stream;

            }
        });
    }
}

解决方案

Hi please try the below reference link for Progressbar tutorials.Hope it should helpful for you. You can download the sample source code and try it in your application.

Progress Bar

Progress bar for downloading a file

Android Progress Bar Example

Hi here i added the sample download file from server code. Please check and let me know.

    import java.io.InputStream;
    import java.net.URL;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.net.URLConnection; 
    import android.app.Dialog;
    import android.graphics.drawable.Drawable;
    import android.os.AsyncTask;
    import android.os.Environment;
    import android.widget.ImageView;

    public class download extends Activity {

        // button to show progress dialog
        Button btnShowProgress;

        // Progress Dialog
        private ProgressDialog pDialog;
        ImageView my_image;
        // Progress dialog type (0 - for Horizontal progress bar)
        public static final int progress_bar_type = 0; 

        // File url to download
        private static String file_url = "http://alshbab90.softsmedia.com/helmi_1.mp3";

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

        // show progress bar button
        btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
        // Image view to show image after downloading
        my_image = (ImageView) findViewById(R.id.my_image);
        /**
         * Show Progress bar click event
         * */
          btnShowProgress.setOnClickListener(new View.OnClickListener()   { 

            @Override
            public void onClick(View v) {
            // starting new Async Task
            new DownloadFileFromURL().execute(file_url);
            }
        });
        }

        /**
         * Showing Dialog
         * */
        @Override
        protected Dialog onCreateDialog(int id) {
        switch (id) {
        case progress_bar_type: // we set this to 0
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            return pDialog;
        default:
            return null;
        }
        }

        /**
         * Background Async Task to download file
         * */
        class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream
            OutputStream output = new FileOutputStream("/sdcard/helmi_1.mp3");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

            } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
           }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
             dismissDialog(progress_bar_type);

             // Displaying downloaded image into image view
             // Reading image path from sdcard
               String imagePath =  Environment.getExternalStorageDirectory().toString() + "/helmi_1.mp3";
             // setting downloaded into image view
            my_image.setImageDrawable(Drawable.createFromPath(imagePath));
        }

        }
    }

Create main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- Download Button -->
<Button android:id="@+id/btnProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Download File with Progress Bar"
    android:layout_marginTop="50dip"/>

<!-- Image view to show image after downloading -->
<ImageView android:id="@+id/my_image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

Need to add AndroidManifest.xml permission as,

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope it should helpful for you. Please try and let me know. Thanks

这篇关于Android的使用进度为音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆