Get it on google play

كيفية تشغيل مقطع صوتي بين 2 اكتيفيتي



  • السلام عليكم،

    ما هي الطريقة الصحيحة لتشغيل صوت ما بين 2 اكتيفيتي.

    بحيث يتم اختيار الصوت من ريسايكلر فيو في الشاشة الاولى وارسال المعلومات الى الشاشة الثانية التي فيها مشغل الصوت ليشتغل المقطع الصوتي مباشرة.

    ارجو ان يكون السؤال واضح.

    بارك الله فيكم.


  • administrators

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
    
    
    }
    public void onClick(View v)
    {
        int soundfile =R.raw.sample;
        Intent intent = new Intent(getBaseContext(), Main2Activity.class);
        intent.putExtra("SOUND_FILE", soundfile);
        startActivity(intent);
    }
    

    public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    
        int mp3 = getIntent().getIntExtra("SOUND_FILE",0);
    
        MediaPlayer mp = MediaPlayer.create(this,mp3);
        mp.start();
    }
    

    }
    }




  • administrators

    @apitos

    على فكرة انت ممكن تنسخ الكود هنا عادى بدون صورة
    او لو صورة تضغط على
    print scrn
    فى لوحة المفاتيح وبعدين
    paste
    هنا فى الموضوع

    فارجو نسخ الكود كاملا
    وعموما انت هنا استخدمت مكان الاغنية مسلسل الاغنية وليس رقم الاغنية نقسها
    المفترض انك قمت بارسال بيانات الاغانى فى
    array الى الـ
    adpter
    وبالتالى يمكنك احضار رقم الاغنية من المصفوفة عن طريق المسلسل
    )position)



  • تفضل AudioAdapter.java

    package com.example.android.musicplayertest;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;

    /**

    • Created by Apt on 15/05/2018.
      */

    public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<AudioModel> mList;
    
    AudioAdapter(Context context, ArrayList<AudioModel> list) {
        mContext = context;
        mList = list;
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
        ImageView item_image;
        TextView item_artistName, item_songTitle;
    
        public ViewHolder(View itemView) {
            super(itemView);
    
            item_image = itemView.findViewById(R.id.item_image);
            item_artistName = itemView.findViewById(R.id.item_artistName);
            item_songTitle = itemView.findViewById(R.id.songTitle);
    
        }
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.rv_audio_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
    
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
    
        final AudioModel audioItem = mList.get(position);
    
        ImageView image = holder.item_image;
        TextView artistName, songTitle;
    
        artistName = holder.item_artistName;
        songTitle = holder.item_songTitle;
    
    
        image.setImageResource(audioItem.getImage());
    
        artistName.setText(audioItem.getArtistName());
        songTitle.setText(audioItem.getSongTitle());
    
    
    
           holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "**" +audioItem, Toast.LENGTH_SHORT).show();
    
                Intent intent = new Intent(mContext, MainActivity.class);
               mContext.startActivity(intent);
    
                private int currentPosition = 0;
    
                currentPosition = position;
                Intent i = new Intent(mContext, MainActivity.class);
                //String song = AudioModel.class.getsongId(currentPosition);
                String song = mList.get(position);
                //i.putExtra("position", currentPosition);
               i.putExtra("song", song);
               mContext.startActivity(i);
    
            }
        });
    
    }
    
    @Override
    public int getItemCount() {
        return mList.size();
    }
    

    }



  • تفضل AudioAdapter.java

    package com.example.android.musicplayertest;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;

    /**

    • Created by Apt on 15/05/2018.
      */

    public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<AudioModel> mList;
    
    AudioAdapter(Context context, ArrayList<AudioModel> list) {
        mContext = context;
        mList = list;
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
        ImageView item_image;
        TextView item_artistName, item_songTitle;
    
        public ViewHolder(View itemView) {
            super(itemView);
    
            item_image = itemView.findViewById(R.id.item_image);
            item_artistName = itemView.findViewById(R.id.item_artistName);
            item_songTitle = itemView.findViewById(R.id.songTitle);
    
        }
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.rv_audio_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
    
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
    
        final AudioModel audioItem = mList.get(position);
    
        ImageView image = holder.item_image;
        TextView artistName, songTitle;
    
        artistName = holder.item_artistName;
        songTitle = holder.item_songTitle;
    
    
        image.setImageResource(audioItem.getImage());
    
        artistName.setText(audioItem.getArtistName());
        songTitle.setText(audioItem.getSongTitle());
    
    
    
           holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "**" +audioItem, Toast.LENGTH_SHORT).show();
    
                Intent intent = new Intent(mContext, MainActivity.class);
               mContext.startActivity(intent);
    
                private int currentPosition = 0;
    
                currentPosition = position;
                Intent i = new Intent(mContext, MainActivity.class);
                //String song = AudioModel.class.getsongId(currentPosition);
                String song = mList.get(position);
                //i.putExtra("position", currentPosition);
               i.putExtra("song", song);
               mContext.startActivity(i);
    
            }
        });
    
    }
    
    @Override
    public int getItemCount() {
        return mList.size();
    }
    

    }



  • السلام عليكم،

    هل من تصحيح ؟


  • administrators

    @apitos
    وعليكم السلام اخى الفاضل عذرا على التاخير فاالعمل مع صلاة التهجد والتروايح ياخذان كل الوقت تقريبا سارد عليك باذن الله خلال الايام القادمة


  • administrators

    السلام عليكم ارجو ارفاق باقى الكود كاملا مع ملفات الـ
    xml



  • وعليكم السلام ورجمة الله

    تقبل الله منا ومنكم

    هناك الملفات التالية :

    • AudioList
    • rv_item_list
    • AudioAdapter
    • MainActivity (MediaPlayer)

    هل تحتاجهم كلهم ؟


  • administrators

    نعم قم بنسخه هنا كل ملف فى تعليق لوحده



  • ساحاول نسخهم غدا ان شاء الله عندما اعود الى المنزل 🙂



  • activity_audio_list.xml :

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

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".AudioList">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/blue"
        android:theme="@style/AppThemeCustomize">
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
    
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleMarginStart="78dp"
            app:expandedTitleMarginEnd="124dp"
            >
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:contentDescription="@string/app_name"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/nature"
                app:layout_collapseMode="pin"
                app:layout_collapseParallaxMultiplier="0.7" />
    
            <android.support.v7.widget.Toolbar
    
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
    
                android:background="@color/toolbarTransparent"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/AppThemeCustomize"
                />
    
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_collapseMode="parallax">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    </android.support.design.widget.CoordinatorLayout>



  • AudioList.java :

    package com.example.android.musicplayertest;

    import android.support.design.widget.CollapsingToolbarLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;

    import java.util.ArrayList;

    public class AudioList extends AppCompatActivity {

    RecyclerView recyclerView;
    ArrayList<AudioModel> audioList;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_list);
    
    
        /****/
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("Album");
    

    //this will change the display title text color.

        collapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));
        collapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.white));
    
        final Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        /****/
    
        recyclerView = findViewById(R.id.rv);
    
    
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        RecyclerView.LayoutManager rvLiLayoutManager = layoutManager;
        recyclerView.setLayoutManager(rvLiLayoutManager);
    
        audioList = new ArrayList<AudioModel>();
    
        audioList.add(new AudioModel(R.drawable.disc, "Rojas", "Celeste", R.raw.leo_rojas_celeste));
        audioList.add(new AudioModel(R.drawable.disc, "Rojas", "Chaksi", R.raw.leo_rojas_chaski));
        audioList.add(new AudioModel(R.drawable.disc, "Rojas", "Der Ainsame", R.raw.leo_rojas_der_einsame_hirte));
        audioList.add(new AudioModel(R.drawable.disc, "Rojas", "El Condor", R.raw.leo_rojas_el_condor_pasa));
    
        AudioAdapter adapter = new AudioAdapter(this, audioList);
        recyclerView.setAdapter(adapter);
    
    
    }
    

    }



  • activity_main.xml :

    <?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="@color/player_background"
    tools:context="com.example.android.musicplayertest.MainActivity">

    <!-- Song Thumbnail Image -->
    
    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="250dp"
        android:layout_height="250dp"
    
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="20dp"
        android:alpha="0.9"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/leo_rojas"
        app:civ_border_color="@color/black"
        app:civ_border_width="1dp" />
    
    <!-- Player Header -->
    <LinearLayout
        android:id="@+id/player_header_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/profile_image"
        android:background="@null"
        android:orientation="vertical"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">
    
        <!-- Song Title -->
        <TextView
            android:id="@+id/songTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:fontFamily="@string/font_family_thin"
            android:paddingLeft="10dp"
            android:text="El Condor Pasa"
    
            android:textSize="20sp"
            android:textStyle="bold" />
        <!-- Singer Name -->
        <TextView
            android:id="@+id/singerName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:fontFamily="@string/font_family_thin"
            android:paddingLeft="0dp"
            android:text="Leo Rojas"
    
            android:textSize="16sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    <!-- Repeat / Shuffle buttons -->
    <LinearLayout
        android:id="@+id/repeatShuffle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        android:layout_below="@id/player_header_bg"
        android:gravity="center">
    
        <!-- Repeat Button -->
        <ImageButton
            android:id="@+id/btnRepeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:background="@null"
            android:src="@drawable/ic_shuffle_black_24dp" />
    
        <!-- Shuffle Button -->
        <ImageButton
            android:id="@+id/btnShuffle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:background="@null"
            android:src="@drawable/ic_repeat_black_24dp" />
    </LinearLayout>
    
    <!-- Timer Display -->
    <LinearLayout
        android:id="@+id/timerDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/repeatShuffle"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:weightSum="10">
    
        <!-- Current Duration Label -->
        <TextView
            android:id="@+id/songCurrentDurationLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0"
            android:paddingLeft="20dp"
            android:text="00:00"
            android:textStyle="normal" />
    
        <!-- SoundBar -->
        <SeekBar
            android:id="@+id/songProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="9"
            android:padding="0dp"
            android:paddingEnd="8dp"
            android:paddingStart="8dp" />
    
        <!-- Total Duration Label -->
        <TextView
            android:id="@+id/songRemainingDurationLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0"
            android:gravity="right"
            android:paddingRight="20dp"
            android:text="- 04:00"
    
            />
    
    </LinearLayout>
    
    <!-- Progress Bar/Seek bar -->
    <!--<SeekBar-->
    <!--android:id="@+id/songProgressBar"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_below="@id/timerDisplay"-->
    <!--android:layout_marginLeft="60dp"-->
    <!--android:layout_marginRight="60dp"-->
    <!--android:max="100"-->
    
    <!--android:padding="0dp"-->
    <!--android:paddingEnd="0dp"-->
    <!--android:paddingStart="0dp"-->
    <!--android:progress="30" />-->
    
    
    <!-- Player Buttons -->
    <LinearLayout
        android:id="@+id/player_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timerDisplay"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="20dp"
        android:background="@null"
        android:gravity="center">
    
        <!-- Player Buttons -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
    
            <!-- Previous Button -->
            <ImageView
                android:id="@+id/btnPrevious"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@null"
                android:src="@drawable/ic_skip_previous_black_24dp" />
    
            <!-- Backward Button -->
            <!--<ImageButton-->
            <!--android:id="@+id/btnBackward"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:background="@null"-->
            <!--android:src="@drawable/ic_play_arrow" />-->
    

    <LinearLayout
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:gravity="center"
    android:background="@drawable/circlebackground"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp">
    <!-- Play Button -->
    <ImageView
    android:id="@+id/btnPlay"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:src="@drawable/ic_play_white_24dp" />
    </LinearLayout>

            <!-- Forward Button -->
            <!--<ImageButton-->
            <!--android:id="@+id/btnForward"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:background="@null"-->
            <!--android:src="@drawable/ic_play_arrow" />-->
    
            <!-- Next Button -->
            <ImageView
                android:id="@+id/btnNext"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@null"
                android:src="@drawable/ic_skip_next_black_24dp" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/soundEffect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/player_buttons"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">
    
        <!-- Mute Button -->
        <ImageButton
            android:id="@+id/btnMute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:paddingLeft="20dp"
            android:layout_gravity="center"
            android:layout_weight="0"
            android:background="@null"
            android:src="@drawable/ic_volume_off_black_24dp" />
    
        <!-- SoundBar -->
        <SeekBar
            android:id="@+id/volumeProgressBar"
    
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="10"
            android:padding="0dp"
            android:paddingEnd="8dp"
            android:paddingStart="8dp"
            android:progress="50"/>
    
        <!-- Speaker Button -->
        <ImageButton
            android:id="@+id/btnSpeaker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:paddingRight="20dp"
            android:layout_gravity="center"
            android:layout_weight="0"
            android:background="@null"
            android:src="@drawable/ic_volume_up_black_24dp" />
    </LinearLayout>
    

    </RelativeLayout>



  • Mainactivity.java :

    package com.example.android.musicplayertest;

    import android.annotation.SuppressLint;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.lang.reflect.Field;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {

    private ImageView playButton;
    private SeekBar progressBar, volumeBar;
    private TextView elapsedTimeLable, remainingTimeLable;
    private MediaPlayer mediaPlayer;
    private int totalTime;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        playButton = (ImageView) findViewById(R.id.btnPlay);
        elapsedTimeLable = (TextView) findViewById(R.id.songCurrentDurationLabel);
        remainingTimeLable = (TextView) findViewById(R.id.songRemainingDurationLabel);
    
        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.leo_rojas_der_einsame_hirte);
        //mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.leo_rojas_el_condor_pasa);
        //mediaPlayer.setLooping(true);
        mediaPlayer.seekTo(0);
        mediaPlayer.setVolume(0.5f, 0.5f);
    
        /* mediaPlayer.start(); */
        totalTime = mediaPlayer.getDuration();
        progressBar = (SeekBar) findViewById(R.id.songProgressBar);
        //Toast.makeText(this, "TotalTime = "+totalTime, Toast.LENGTH_SHORT).show();
        progressBar.setMax(totalTime);
    
        progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mediaPlayer.seekTo(progress);
                    progressBar.setProgress(progress);
    
                    //----------------
                    if (progress == 0) {
                        playButton.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                    }
                    if (progress == totalTime) {
                        playButton.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                    }
                    //--------------------
                }
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
        });
    
        volumeBar = (SeekBar) findViewById(R.id.volumeProgressBar);
        volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float volumeNumber = progress / 100f;
                mediaPlayer.setVolume(volumeNumber, volumeNumber);
    
    
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
        });
    
    
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                    //playButton.setBackgroundResource(R.drawable.ic_pause_black_24dp);
                    playButton.setImageResource(R.drawable.ic_pause_white_24dp);
                } else {
                    mediaPlayer.pause();
                    //playButton.setBackgroundResource(R.drawable.ic_play_arrow_black_24dp);
                    playButton.setImageResource(R.drawable.ic_play_white_24dp);
                }
            }
        });
    
    
        /**********/
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mediaPlayer != null) {
                    try {
                        Message message = new Message();
    
                        message.what = mediaPlayer.getCurrentPosition();
    
                        handler.sendMessage(message);
    
                        Thread.sleep(1000);
    
    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    
    
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            int currentPosition = message.what;
            progressBar.setProgress(currentPosition);
    
            String elapsedTime = createTimeLabel(currentPosition);
            elapsedTimeLable.setText(elapsedTime);
    
            String remainTime = createTimeLabel(totalTime - currentPosition);
            remainingTimeLable.setText("- " + remainTime);
        }
    };
    
    public String createTimeLabel(int time) {
        String timeLabel = "";
        int min = time / 1000 / 60;
        int sec = time / 1000 % 60;
    
        timeLabel = min + ":";
        if (sec < 10) {
            timeLabel += "0";
            timeLabel += sec;
        } else {
            timeLabel += sec;
        }
    
    
        return timeLabel;
    
    }
    

    // public void getRawFiles() {
    // String sName;
    // String sTitle = "";
    //
    // Field[] fields = R.raw.class.getFields();
    //// loop for every file in raw folder
    // for (int count = 0; count < fields.length; count++) {
    //
    // int rid = fields[count].getInt(fields[count]);
    //
    // // Use that if you just need the file name
    // String filename = fields[count].getName();
    //
    // String arrInfo[] = filename.split("_");
    // sName = arrInfo[0] + arrInfo[1];
    // for (int count2 = 2; count2 < arrInfo.length; count2++) {
    // sTitle = sTitle + arrInfo[count2];
    //
    // }
    //
    // //List.add(sName, sTitle);
    //
    //
    // }
    // }
    }



  • rv_audio_items.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:weightSum="10">
    
        <ImageView
            android:id="@+id/item_image"
            android:layout_width="0dp"
            android:layout_height="128dp"
            android:layout_weight="4"
            android:scaleType="centerCrop"
            android:src="@drawable/leo_rojas" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:orientation="vertical"
            android:paddingLeft="10dp">
    
            <TextView
                android:id="@+id/item_songTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Audio Name"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@+id/item_artistName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Audio Place"
                android:textSize="14sp" />
    
        </LinearLayout>
    
        <!--TextView
            android:id="@+id/item_price"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="2"
            android:background="@drawable/capsule"
            android:gravity="center"
            android:text="Price"
            android:textColor="@color/colorPrimary" /-->
    
    </LinearLayout>
    

    </android.support.v7.widget.CardView>



  • AudioModel.java :

    package com.example.android.musicplayertest;

    /**

    • Created by Apt on 16/05/2018.
      */

    public class AudioModel {

    private int image;
    private String artistName, songTitle;
    private int songId;
    
    public AudioModel(int image, String artistName, String songTitle, int songId) {
        this.image = image;
        this.artistName = artistName;
        this.songTitle = songTitle;
                this.songId = songId;
    }
    
    public int getImage() {
        return image;
    }
    
    public String getArtistName() {
        return artistName;
    }
    
    public String getSongTitle() {
        return songTitle;
    }
    
    public int getSongId() {
        return songId;
    }
    

    }



  • AudioAdapter.java :

    package com.example.android.musicplayertest;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;

    /**

    • Created by Apt on 15/05/2018.
      */

    public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<AudioModel> mList;
    
    AudioAdapter(Context context, ArrayList<AudioModel> list) {
        mContext = context;
        mList = list;
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
        ImageView item_image;
        TextView item_artistName, item_songTitle;
    
        public ViewHolder(View itemView) {
            super(itemView);
    
            item_image = itemView.findViewById(R.id.item_image);
            item_artistName = itemView.findViewById(R.id.item_artistName);
            item_songTitle = itemView.findViewById(R.id.songTitle);
    
        }
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.rv_audio_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
    
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
    
        final AudioModel audioItem = mList.get(position);
    
        ImageView image = holder.item_image;
        TextView artistName, songTitle;
    
        artistName = holder.item_artistName;
        songTitle = holder.item_songTitle;
    
    
        image.setImageResource(audioItem.getImage());
    
        artistName.setText(audioItem.getArtistName());
        songTitle.setText(audioItem.getSongTitle());
    
    
    
    
    
           holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "**" +audioItem, Toast.LENGTH_SHORT).show();
    
                Intent intent = new Intent(mContext, MainActivity.class);
               mContext.startActivity(intent);
    
                private int currentPosition = 0;
    
                currentPosition = position;
                Intent i = new Intent(mContext, MainActivity.class);
                //String song = AudioModel.class.getsongId(currentPosition);
                String song = mList.get(position);
                //i.putExtra("position", currentPosition);
               i.putExtra("song", song);
               mContext.startActivity(i);
    
            }
        });
    
    }
    
    @Override
    public int getItemCount() {
        return mList.size();
    }
    

    }



  • @hassan-youssef

    لقد تم وضع الملفات المطلوبة

    🙂



Suggested Topics